package com.syxp.pda.service.base.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.dao.BaseDao;
import com.syxp.pda.core.service.impl.BaseServiceImpl;
import com.syxp.pda.core.util.ArraysBuilder;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.base.DepartmentService;

@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService{

	public DepartmentServiceImpl(BaseDao<Department> baseDao){
		super(baseDao) ;
	}
	
	public Department loadDutyClass(int departmentID) {
		Department department = getDao().get(departmentID) ;
		department.setDutys(ArraysBuilder.buildSet(department.getDutys())) ;
		department.setClassDutys(ArraysBuilder.buildSet(department.getClassDutys())) ;
		return department ;
	}
	
	@Override
	public Set<User> getUsers(int departmentId) {
		Set<User> set = (Set<User>)ArraysBuilder.buildSet(this.loadService(departmentId).getUsers());
		return set;
	}

	@Override
	public List<User> getUserWithRole(int departID) {
		List<User> list = new ArrayList<User>();
		List<User> all = new ArrayList<User>();
		all.addAll(this.loadService(departID).getUsers());
		for(User u: all) {
			Set roleSet = u.getRoles();
			if(roleSet.size()>0) {
				User user = new User();
				user.setUserID(u.getUserID());
				user.setOperatorCode(u.getOperatorCode());
				user.setUserName(u.getUserName());
				user.setUserAccount(u.getUserAccount());
				user.setRemarks(u.getRemarks());
				user.setRoles(ArraysBuilder.buildSet(roleSet));
				list.add(user);
			}
		}
		return list;
	}
	
	@Override
	public Set<Duty> getDutys(int departmentId) {
		Set<Duty> set = ArraysBuilder.buildSet(this.loadService(departmentId).getDutys());
		return set;
	}
}